# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        def reverse(head):
            if head.next is None:
                return head
            last = reverse(head.next)
            head.next.next = head
            head.next = None
            return last

        return None if head is None else reverse(head)
